package com.sunshine.springboot.controller;

import com.sunshine.springboot.bean.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Demo class
 *
 * @author WeiHong
 * @date 2018/8/13
 */
@RestController
@RequestMapping("hello")
public class HelloController {

    private final Map<String, Object> params = new HashMap<>(5);

    @Value("${my.age}")
    private String age;
    @Value("${my.name}")
    private String name;

    @RequestMapping("test")
    public Map<String, Object> test() {
        params.clear();
        params.put("age", age);
        params.put("name", name);
        return params;
    }

    /**
     * 测试 restful 协议，重路径中获取字段
     *
     * @param cityId
     * @param userId
     * @return
     */
    @GetMapping("getParams/{city_id}/{user_id}")
    public Map<String, Object> getParams(@PathVariable("city_id") String cityId, @PathVariable("user_id") String userId) {
        params.clear();
        params.put("city_id", cityId);
        params.put("user_id", userId);
        return params;
    }

    /**
     * 测试默认值
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("list_limit")
    public Map<String, Object> listLimit(@RequestParam(defaultValue = "0", name = "page") int page, @RequestParam(defaultValue = "10", name = "size") int size) {
        params.clear();
        params.put("page", page);
        params.put("size", size);
        return params;
    }

    /**
     * bean 对象传参
     * @param user
     * @return
     */
    @GetMapping("save_user")
    public Map<String, Object> saveUser(User user) {
        params.clear();
        params.put("user", user);
        return params;
    }

    /**
     * 测试获取http头信息
     * @param accessToken
     * @param id
     * @return
     */
    @GetMapping("get_header")
    public Map<String, Object> getHeader(@RequestHeader("access_token") String accessToken, String id){
        params.clear();
        params.put("access_token", accessToken);
        params.put("id", id);
        return params;
    }
}
